package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.dao.SysFileinfoAuthorizeDao;
import com.open.capacity.reimburse.dao.SysFileinfoDao;
import com.open.capacity.reimburse.entity.SysFileinfo;
import com.open.capacity.reimburse.entity.SysFileinfoAuthorize;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.StringUtil;
import com.open.capacity.utils.SysUserUtil;
import com.open.capacity.workflow.model.WFSchemeAuthorize;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.SysFilefolder;
import com.open.capacity.reimburse.dao.SysFilefolderDao;
import com.open.capacity.reimburse.service.SysFilefolderService;
import org.springframework.util.Assert;


@Service
public class SysFilefolderServiceImpl implements SysFilefolderService {

    @Autowired
    private SysFilefolderDao sysFilefolderDao;

    @Autowired
    private SysFileinfoAuthorizeDao fileinfoAuthorizeDao;

    @Autowired
    private SysFileinfoDao fileinfoDao;

    /**
     * 添加
     *
     * @param sysFilefolder
     */
    public int save(SysFilefolder sysFilefolder) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
        sysFilefolder.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        sysFilefolder.setCreateTime(new Date());
        sysFilefolder.setCreateUserid(loginUser.getId());
        sysFilefolder.setCreateUsername(loginUser.getRealName());
        sysFilefolder.setUpdateTime(new Date());
        sysFilefolder.setUpdateUserid(loginUser.getId());
        sysFilefolder.setUpdateUsername(loginUser.getRealName());
        if (StringUtil.isEmpty(sysFilefolder.getParentId())) {
            sysFilefolder.setParentId("");
        }
        if (sysFilefolder.getParentId().equals("0")) {
            sysFilefolder.setParentId("");
        }
        sysFilefolder.setStatus(1);
        return sysFilefolderDao.save(sysFilefolder);
    }

    /**
     * 修改
     *
     * @param sysFilefolder
     */
    public int update(SysFilefolder sysFilefolder) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
        sysFilefolder.setUpdateTime(new Date());
        sysFilefolder.setUpdateUserid(loginUser.getId());
        sysFilefolder.setUpdateUsername(loginUser.getRealName());
        return sysFilefolderDao.update(sysFilefolder);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        List<SysFileinfo> fileinfoList = fileinfoDao.findByFolderId(id);
        Assert.isTrue(CollectionUtils.isEmpty(fileinfoList), "该文件夹下存在文件，不允许直接删除");
        return sysFilefolderDao.updateStatus(id, -1);
    }

    @Override
    public int restore(String id) {
        return sysFilefolderDao.updateStatus(id, 1);
    }

    @Override
    public int thoroughDelete(String id) {
        return sysFilefolderDao.delete(id);
    }

    @Override
    public int emptyRecycled() {
        return sysFilefolderDao.emptyRecycled();
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public SysFilefolder findById(String id) {
        return sysFilefolderDao.findById(id);
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<SysFilefolder> findAll(Map<String, Object> params) {
        return sysFilefolderDao.findAll(params);
    }

    @Override
    public int share(String id, List<SysFileinfoAuthorize> fileinfoAuthorizes, Integer isShare) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
        SysFilefolder filefolder = sysFilefolderDao.findById(id);
        Assert.isTrue(loginUser.getId().equals(filefolder.getCreateUserid()),"您无权分享该文件夹");

        fileinfoAuthorizeDao.deleteByFileinfoId(id);
        for (SysFileinfoAuthorize authorize : fileinfoAuthorizes) {
            authorize.setFileinfoId(id);
            fileinfoAuthorizeDao.save(authorize);
        }
        filefolder.setIsShare(isShare);
        filefolder.setShareTime(new Date());
        int count = sysFilefolderDao.update(filefolder);
        return count;
    }

    @Override
    public int move(String id, String moveFolderId) {
        SysFilefolder filefolder = sysFilefolderDao.findById(id);
        filefolder.setParentId(moveFolderId);
        int count = sysFilefolderDao.update(filefolder);
        return count;
    }
}
